
<?php 
//require_once 'library/MySQLBackupService.php';
require_once 'library/JSONQuery.php';
require_once 'library/Utilities.php';
$util = new Utilities();

require_once 'library/MySQLConnect.php';
$mysqli = new MySQLConnect('localhost', 'root', 'fred');

//$backup = new MySQLBackupService('localhost', 'root', 'fred', 'test');
$flexJSON = '[{"database":"test","table":"comments","objectKey":"3","tableKey":"id","objectValue":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sed tortor. Integer aliquam adipiscing lacus. Ut nec urna et arcu imperdiet ullamcorper.","objectName":"comment_body"},{"database":"test","table":"comments","objectKey":"2","tableKey":"id","objectValue":"Rio is being a faggot","objectName":"comment_body"},{"database":"test","table":"comments","objectKey":"2","tableKey":"id","objectValue":"2009-01-08 14:28:42","objectName":"comment_created"},{"database":"test","table":"comments","objectKey":"7","tableKey":"id","objectValue":"This is bull shit","objectName":"comment_body"},{"database":"test","table":"comments","objectKey":"7","tableKey":"id","objectValue":"2009-01-08 14:28:43","objectName":"comment_created"}]';
$jquery = new JSONQuery($mysqli);


$util->readLog();
$mysqli->dump( 'getServer', $mysqli->getServer() );
$mysqli->dump( 'getDatabases', $mysqli->getDatabases() );
$mysqli->dump( 'json_encodetree_getSchemas', json_encode( $mysqli->tree_getTree() ));
$mysqli->dump( 'tree_getSchemas', $mysqli->tree_getTree() );





//COLUMN CHILDREN
$c1 = array( 'label' => 'user_id' );
$c2 = array( 'label' => 'user_email' );
$c3 = array( 'label' => 'user_pass' );

//TABLE CHILDREN
$columns = array( 'label' => 'Columns', 'children' => array( $c1, $c2, $c3 ) );
$constraints = array( 'label' => 'Constraints', 'children' => array( 'label' => 'user_roles' ) );
$dependencies = array( 'label' => 'Dependencies', 'children' => array( 'label' => 'admin table' ) );
$indexes = array( 'label' => 'Indexes', 'children' => array( 'label' => 'user_id' ) );
$triggers = array( 'label' => 'Triggers', 'children' => array( 'label' => 'thisTrigger' ) );

//TABLE FOLDER
$t1 = array( 'label' => 'users', 'children' => array( $columns, $constraints, $dependencies, $indexes, $triggers ) );
$t2 = array( 'label' => 'posts', 'children' => array( $columns, $constraints, $dependencies, $indexes, $triggers ) );
$t3 = array( 'label' => 'contacts', 'children' => array( $columns, $constraints, $dependencies, $indexes, $triggers ) );

//DATABASE CHILDREN
$tables = array( 'label' => 'Tables', 'children' => array( $t1, $t2, $t3 ) );
$storedprocs = array( 'label' => 'Stored Procedures', 'children' => array( 'label' => 'stored1' ) );
$userfunctions = array( 'label' => 'User-Definded Functions', 'children' => array( 'label' => 'callWeather' ) );
$views = array( 'label' => 'Views', 'children' => array( 'label' => 'usersView' ) );

//DATABASE FOLDER
$databases = array( $dependencies, $storedprocs, $tables, $userfunctions, $views );

//SCHEMA CHILDREN
$d1 = array( 'label' => 'textpattern (DATABASE)', 'children' => $databases );
$d2 = array( 'label' => 'wordpress (DATABASE)', 'children' => $databases );
$d3 = array( 'label' => 'media (DATABASE)', 'children' => $databases );

//SCHEMA FOLDER
$schemasArr = array( 'label' => 'Schemas (SCHEMA_LEVEL)', 'children' => array( $d1, $d2, $d3 ) );

//HOST
$hostArr[] = array( 'label' => 'localhost(MySQL v. 5.0.41)', 'children' => $schemasArr );

//ROOT
$tree[] = array( 'label' => 'SQL Databases', 'children' => $hostArr );

//$util->dump( 'Connection Tree', json_encode( $tree ) );
//$util->dump( 'Connection Tree', $tree );




















$util->dump( 'INSERT JSON QUERY', $jquery->buildQuery( $flexJSON, 'INSERT', false ) );
$util->dump( 'UPDATE JSON QUERY', $jquery->buildQuery( $flexJSON, 'UPDATE', false ) );
$util->dump( 'DELETE JSON QUERY', $jquery->buildQuery( $flexJSON, 'DELETE', false ) );
//$util->dump( 'SELECT JSON QUERY', $jquery->buildQuery( $flexJSON, 'SELECT', false ) );

$util->dump( 'analyzeQuery', $mysqli->analyzeQuery( 'SELECT * FROM test.users LIMIT 10' ) );
//$util->dump( 'SELECT', $mysqli->execute( 'SELECT * FROM test.users' ) );





$mysqli = new mysqli("localhost", "root", "fred", "test");

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT * FROM users LIMIT 5;";
$query .= "SELECT * FROM categories LIMIT 5;";
$query .= "SELECT * FROM posts LIMIT 5";

/* execute multi query */
if ( $mysqli->multi_query( $query ) ) 
{
    do 
    {
        
		/* store first result set */
        if ( $result = $mysqli->store_result() ) 
        {
            while ( $row = $result->fetch_assoc() ) 
            {
            	$query1[] = $row;
            }
            
            $result->close();
            
            
            $util->dump( 'Multi-Query', $query1 );
        }
        
        
        
        /* print divider */
        if ( $mysqli->more_results() ) 
        {
            printf( "-----------------\n" );
        }
    } 
    while ( $mysqli->next_result() );
}

/* close connection */
$mysqli->close();






















function getKeys( $array )
{
	$argKeys = array();
	
	foreach( $array as $value )
	{
		if ( is_array( $value ) )
		{
			$argKeys = getArgKeys( $value );
		}
	}
	$keys = array_keys( $array );
	
	
	return array_merge( $argKeys, $keys );
}

function getArgKeys( $array )
{
	$argKeys = array();
	$argKeys = array_keys( $array );

	return $argKeys;
}


$args = array( 'id' => 2, 'user_email' => 'jonniedollas@gmail.com', 'user_name' => 'Jonnie' );
$array = array( 'mode' => 'insert', 'database' => 'test', 'table' => 'users', 'args' => $args );
$util->dump( 'Query Keys', getKeys( $array ) );


function mysql_dump( $database ) 
{

      $query = '';

      $tables = @mysql_list_tables( $database );
      while ($row = @mysql_fetch_row($tables)) { $table_list[] = $row[0]; }

      for ($i = 0; $i < @count($table_list); $i++) {

         $results = mysql_query('DESCRIBE ' . $database . '.' . $table_list[$i]);

         $query .= 'DROP TABLE IF EXISTS `' . $database . '.' . $table_list[$i] . '`;';
         $query .= '<br/>' . 'CREATE TABLE `' . $database . '.' . $table_list[$i] . '` (';

         $tmp = '';

         while ($row = @mysql_fetch_assoc($results)) {

            $query .= '`' . $row['Field'] . '` ' . $row['Type'];

            if ($row['Null'] != 'YES') { $query .= ' NOT NULL'; }
            if ($row['Default'] != '') { $query .= ' DEFAULT \'' . $row['Default'] . '\''; }
            if ($row['Extra']) { $query .= ' ' . strtoupper($row['Extra']); }
            if ($row['Key'] == 'PRI') { $tmp = 'primary key(' . $row['Field'] . ')'; }

            $query .= ','. '<br/>';

         }

         $query .= $tmp . '<br/>' . ');' . str_repeat('<br/>', 2);

         $results = mysql_query('SELECT * FROM ' . $database . '.' . $table_list[$i]);

         while ($row = @mysql_fetch_assoc($results)) {

            $query .= 'INSERT INTO `' . $database . '.' . $table_list[$i] .'` (';

            $data = Array();

            while (list($key, $value) = @each($row)) { $data['keys'][] = $key; $data['values'][] = addslashes($value); }

            $query .= join($data['keys'], ', ') . ')' . '\n' . 'VALUES (\'' . join($data['values'], '\', \'') . '\');' . '<br/>';

         }

         $query .= str_repeat( '<br/>', 2);

      }

      return $query;

   }

//echo mysql_dump( 'test' );

	
?>